﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.Common;
using Ivy.Common.Framework.Base;
using Ivy.Common.Framework.DataAccess;
using Ivy.UserRegistration.Entity;

namespace Ivy.UserRegistration.DataAccess
{
   public class Sys_LoginHistoryLogDao : IDaoInterface<Sys_LoginHistoryLog, Sys_LoginHistoryLogCriteria>
   {
       #region  Variable & constant
       #endregion  Variable & constant

       #region Constructor & Destructor
       public Sys_LoginHistoryLogDao() : base() { }
       #endregion Constructor & Destructor

       #region Properties
       #endregion Properties

       #region Public Methods
       #region IDaoInterface<User, UserCriteria> Members
       public bool Insert(Sys_LoginHistoryLog Sys_LoginHistoryLogEntity)
       {
           bool IsOperationSuccessful = false;
           
           if (Sys_LoginHistoryLogEntity.EntityState == EntityState.New)
               {
                   DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
                   DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
                   CmdObj.CommandText = "Ivy_Sys_LoginHistoryLog_Insert";
                   //CmdObj.CommandText = UserManagementSQLCommandText.InsertUser();
                   CmdObj.CommandType = CommandType.StoredProcedure;
                   //CmdObj.CommandType = CommandType.Text;


                   DataAccessProviderUtility.AddParameter(CmdObj, "@LoginUserKey", ParameterDirection.Input, DbType.Int32, Sys_LoginHistoryLogEntity.LoginUserKey);
                   DataAccessProviderUtility.AddParameter(CmdObj, "@LoginUserLoginID", ParameterDirection.Input, DbType.String, Sys_LoginHistoryLogEntity.LoginUserLoginID);
                   DataAccessProviderUtility.AddParameter(CmdObj, "@LoginStartTime", ParameterDirection.Input, DbType.DateTime, Sys_LoginHistoryLogEntity.LoginStartTime);
                   DataAccessProviderUtility.AddParameter(CmdObj, "@LoginEndTime", ParameterDirection.Input, DbType.DateTime, Sys_LoginHistoryLogEntity.LoginEndTime);
                   DataAccessProviderUtility.AddParameter(CmdObj, "@LoginUserBrowser", ParameterDirection.Input, DbType.String, Sys_LoginHistoryLogEntity.LoginUserBrowser);
                   DataAccessProviderUtility.AddParameter(CmdObj, "@LoginLocation", ParameterDirection.Input, DbType.String, Sys_LoginHistoryLogEntity.LoginLocation);

                   DatabaseCommon.InsertStoredProcForParameterDatEntityBase(CmdObj, Sys_LoginHistoryLogEntity);

                   DataAccessProviderUtility.AddParameter(CmdObj, "@LoginKey", ParameterDirection.Output, DbType.Int32, DBNull.Value);
                   DataAccessProviderUtility.AddParameter(CmdObj, "@Sys_DateOfCreation", ParameterDirection.Output, DbType.DateTime, DBNull.Value);

                   CmdObj.Connection = DbConn;
                   DbConn.Open();
                   CmdObj.Prepare();
                   int _NoOfRecords = CmdObj.ExecuteNonQuery();
                   if (_NoOfRecords == 1)
                   {
                       Sys_LoginHistoryLogEntity.LoginKey = Convert.ToInt32(CmdObj.Parameters["@LoginKey"].Value);
                       Sys_LoginHistoryLogEntity.Sys_DateOfCreation = Convert.ToDateTime(CmdObj.Parameters["@Sys_DateOfCreation"].Value);
                       Sys_LoginHistoryLogEntity.Sys_DateOfModification = Sys_LoginHistoryLogEntity.Sys_DateOfCreation;
                       Sys_LoginHistoryLogEntity.EntityState = EntityState.UnChanged;
                       IsOperationSuccessful = true;
                   }
                   CmdObj.Dispose();
                   DbConn.Close();
               }
               else
               {
                   throw new Exception("For inserting new User entity data to database, [EntityState] attribute should be [New]");
               }
           
           return IsOperationSuccessful;
       }

       public bool Update(Sys_LoginHistoryLog Sys_LoginHistoryLogEntity)
       {
           bool IsOperationSuccessful = false;
           
               if (Sys_LoginHistoryLogEntity.EntityState == EntityState.Changed)
               {
                   DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
                   DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
                   CmdObj.CommandText = "Ivy_User_Update";
                   CmdObj.CommandType = CommandType.StoredProcedure;

                   DataAccessProviderUtility.AddParameter(CmdObj, "@LoginKey", ParameterDirection.Input, DbType.Int32, Sys_LoginHistoryLogEntity.LoginKey);
                   DataAccessProviderUtility.AddParameter(CmdObj, "@LoginUserKey", ParameterDirection.Input, DbType.Int32, Sys_LoginHistoryLogEntity.LoginUserKey);
                   DataAccessProviderUtility.AddParameter(CmdObj, "@LoginUserLoginID", ParameterDirection.Input, DbType.String, Sys_LoginHistoryLogEntity.LoginUserLoginID);
                   DataAccessProviderUtility.AddParameter(CmdObj, "@LoginStartTime", ParameterDirection.Input, DbType.DateTime, Sys_LoginHistoryLogEntity.LoginStartTime);
                   DataAccessProviderUtility.AddParameter(CmdObj, "@LoginEndTime", ParameterDirection.Input, DbType.DateTime, Sys_LoginHistoryLogEntity.LoginEndTime);
                   DataAccessProviderUtility.AddParameter(CmdObj, "@LoginUserBrowser", ParameterDirection.Input, DbType.String, Sys_LoginHistoryLogEntity.LoginUserBrowser);
                   DataAccessProviderUtility.AddParameter(CmdObj, "@LoginLocation", ParameterDirection.Input, DbType.String, Sys_LoginHistoryLogEntity.LoginLocation);

                   DatabaseCommon.UpdateStoredProcForParameterDatEntityBase(CmdObj, Sys_LoginHistoryLogEntity);

                   DataAccessProviderUtility.AddParameter(CmdObj, "@Sys_DateOfModification", ParameterDirection.Output, DbType.DateTime, DBNull.Value);

                   CmdObj.Connection = DbConn;
                   DbConn.Open();
                   CmdObj.Prepare();
                   int _NoOfRecords = CmdObj.ExecuteNonQuery();
                   if (_NoOfRecords == 1)
                   {
                       Sys_LoginHistoryLogEntity.Sys_DateOfModification = Convert.ToDateTime(CmdObj.Parameters["@Sys_DateOfModification"].Value);
                       Sys_LoginHistoryLogEntity.EntityState = EntityState.UnChanged;
                       IsOperationSuccessful = true;
                   }

                   CmdObj.Dispose();
                   DbConn.Close();
               }
               else
               {
                   throw new Exception("For Updating existing Sys_LoginHistory_Log entity data to database, [EntityState] attribute should be [Changed]");
               }
           
           return IsOperationSuccessful;
       }

       public bool Delete(Sys_LoginHistoryLog DataEntity)
       {
           throw new NotImplementedException();
       }

       public Sys_LoginHistoryLog GetByID(string ID)
       {
           throw new NotImplementedException();
       }

       public List<Sys_LoginHistoryLog> GetAll()
       {
           throw new NotImplementedException();
       }

       public List<Sys_LoginHistoryLog> GetByCriteria(Sys_LoginHistoryLogCriteria CriteriaEntity)
       {
           throw new NotImplementedException();
       }
       #endregion

       public Sys_LoginHistoryLog GetByLoginKey(int LoginKey)
       {
           Sys_LoginHistoryLog _SelectedUserLoginHistory = null; //IMPORTANT NOTE:- Just declare the variable never create an instance, create instance only when required.
           
               DbConnection DbConn = DataAccessProviderUtility.CreateConnectionWithConnectionString();
               DbCommand CmdObj = DataAccessProviderUtility.CreateCommand();
               CmdObj.CommandText = "Ivy_Sys_LoginHistoryLog_GetByKey";
               CmdObj.CommandType = CommandType.StoredProcedure;

               DataAccessProviderUtility.AddParameter(CmdObj, "@LoginKey", ParameterDirection.Input, DbType.Int32, Convert.ToInt32(LoginKey));

               CmdObj.Connection = DbConn;
               DbConn.Open();
               CmdObj.Prepare();
               DbDataReader _DataReader = CmdObj.ExecuteReader();

               if (_DataReader.HasRows)
               {
                   if (_DataReader.Read())
                   {
                       _SelectedUserLoginHistory = new Sys_LoginHistoryLog(); //IMPORTANT NOTE:- Create instance only when required.
                       TranslateSys_LoginHistoryLogFieldsFromReaderToEntity(_SelectedUserLoginHistory, _DataReader);
                   }
               }
               else
               {
                   throw new Exception("No UserRole found");
               }
               CmdObj.Dispose();
               DbConn.Close();
           
           return _SelectedUserLoginHistory;
       }
       
       #endregion Public Methods

       #region Events
       #endregion Events

       #region Private Methods
       private void TranslateSys_LoginHistoryLogFieldsFromReaderToEntity(Sys_LoginHistoryLog pSys_LoginHistoryLog, DbDataReader pDbDataReader)
       {
           pSys_LoginHistoryLog.LoginKey = Convert.ToInt32(pDbDataReader["LoginKey"]);
               pSys_LoginHistoryLog.LoginUserKey = Convert.ToInt32(pDbDataReader["LoginUserKey"]);
               pSys_LoginHistoryLog.LoginUserLoginID = Convert.ToString(pDbDataReader["LoginUserLoginID"]);
               pSys_LoginHistoryLog.LoginStartTime = Convert.ToDateTime(pDbDataReader["LoginStartTime"]);
               pSys_LoginHistoryLog.LoginEndTime = Convert.ToDateTime(pDbDataReader["LoginEndTime"]);
               pSys_LoginHistoryLog.LoginUserBrowser = Convert.ToString(pDbDataReader["LoginUserBrowser"]);
               pSys_LoginHistoryLog.LoginLocation = Convert.ToString(pDbDataReader["LoginLocation"]);

               DatabaseCommon.TranslateSystemFieldsFromReaderToEntity(pSys_LoginHistoryLog, pDbDataReader);
           
       }
       #endregion Private Methods

       
   }
}
